#include "FullScreenVideoWidget.h"

#include <qlabel.h>
#include <qpixmap.h>
#include <qgridlayout.h>

#include "qdesktopwidget.h"
#include "qapplication.h"


FullScreenVideoWidget::FullScreenVideoWidget(QWidget *parent) :
   QVideoWidget(parent),
   m_videoIsAvailable(false),
   m_state(QMediaPlayer::StoppedState)
{
   setMinimumWidth( 300);
   setMinimumHeight( 200);
   topLevelWidget()->setWindowFlags(Qt::SplashScreen);

   m_background = new QLabel( this);
   this->setLayout( new QGridLayout());
   layout()->addWidget( m_background);
   m_background->setScaledContents(true);
   m_background->show();

   hide();
}


void FullScreenVideoWidget::onStateChanged(QMediaPlayer::State newState)
{
   m_state = newState;
   updateVisibility();
}


void FullScreenVideoWidget::onVideoAvailableChanged( bool available)
{
   m_videoIsAvailable = available;
   updateVisibility();

   /* when a video is available, previous still image is removed */
   if (available)
   {
      m_background->setPixmap( QPixmap());
   }
}


void FullScreenVideoWidget::setCurrentPixmap(const QString &fullPath)
{
   QPixmap pixmap(fullPath);
   setCurrentPixmap( pixmap);
}

void FullScreenVideoWidget::setCurrentPixmap(const QPixmap & pixmap)
{
   m_background->setPixmap( pixmap);
}

void FullScreenVideoWidget::onMediaChanged()
{
   updateVisibility();
}


void FullScreenVideoWidget::updateVisibility()
{
   if ((m_state != QMediaPlayer::StoppedState) && (m_videoIsAvailable))
   {
      smartShow();
   }
   else
   {
      /* ensure non visibilty */
      topLevelWidget()->hide();
   }
}

void FullScreenVideoWidget::smartShow()
{
   int screenId = 0;  /* use default screen by default */

   if (QApplication::desktop()->screenCount() > 1)
   {
      /* second monitor available. Use it */
      screenId = 1;
   }

   topLevelWidget()->setGeometry( QApplication::desktop()->screenGeometry(screenId) );
   show();
}
